package com.gavin.demo4fresco.module.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.gavin.demo4fresco.R;
import com.gavin.demo4fresco.adapters.MainAdapter;
import com.gavin.demo4fresco.config.GlobalVars;
import com.gavin.demo4fresco.module.navigation.DynamicDemoActivity;
import com.gavin.demo4fresco.module.navigation.RoundDemoActivity;
import com.gavin.demo4fresco.module.navigation.ScaleDemoActivity;
import com.gavin.demo4fresco.module.navigation.ThumbnailDemoActivity;
import com.gavin.demo4fresco.utils.InitUtil;
import com.gavin.demo4fresco.widgets.DividerGridItemDecoration;
import com.gavin.demo4fresco.widgets.DividerItemDecoration;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private RecyclerView mRecyclerView;
    private MainAdapter mMainAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        /**
         * Fresco SimpleDraweeView 的使用
         * Fresco会替你完成:
         * 显示占位图直到加载完成；
         * 下载图片；
         * 缓存图片；
         * 图片不再显示时，从内存中移除；
         */
        View headerView = navigationView.getHeaderView(0);
        SimpleDraweeView draweeView = (SimpleDraweeView) headerView.findViewById(R.id.simpleDraweeView);
        Uri uri = Uri.parse(GlobalVars.URLS[0]);
        draweeView.setImageURI(uri);

        /**
         * RecycleView 的使用
         */
        mMainAdapter = new MainAdapter(this, InitUtil.initPictureObj());
        mRecyclerView = (RecyclerView) findViewById(R.id.content_main_recycleview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mMainAdapter);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        // 设置item动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mMainAdapter.setOnItemClickLitener(listener);

        /**
         * 开源 RecyclerViewItemAnimators 库的简单使用,详情见 https://github.com/gabrielemariotti/RecyclerViewItemAnimators
         * SlideInOutLeftItemAnimator : which applies a slide in/out from/to the left animation
         * SlideInOutRightItemAnimator : which applies a slide in/out from/to the right animation
         * SlideInOutTopItemAnimator : which applies a slide in/out from/to the top animation
         * SlideInOutBottomItemAnimator : which applies a slide in/out from/to the bottom animation
         * ScaleInOutItemAnimator : which applies a scale animation
         * SlideScaleInOutRightItemAnimator : which applies a scale animation with a slide in/out from/to the right animation
         */

        // Appearance animations
        // AlphaAnimatorAdapter animatorAdapter = new AlphaAnimatorAdapter(mMainAdapter,mRecyclerView);
        // mRecyclerView.setAdapter(animatorAdapter);

        // Simple Animators
        // mRecyclerView.setItemAnimator(new SlideInOutLeftItemAnimator(mRecyclerView));
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    private MainAdapter.OnItemClickLitener listener = new MainAdapter.OnItemClickLitener() {
        @Override
        public void onItemClick(View view, int position) {
            Toast.makeText(MainActivity.this, "点击了" + position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onItemLongClick(View view, int position) {
            Toast.makeText(MainActivity.this, "长按了" + position, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch(id) {
            case R.id.action_add_data:
                mMainAdapter.addData(1);
                break;
            case R.id.action_remove_data:
                mMainAdapter.removeData(1);
                break;
            case R.id.action_linear_layout_manager:
                mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
                mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
                break;
            case R.id.action_grid_layout_manager:
                mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
                mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
                break;
            case R.id.action_staggered_layout_manager_horizontal:
                mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL));
                break;
            case R.id.action_staggered_layout_manager_vertical:
                mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        switch (id) {
            case R.id.kinds_of_scale:
                jump2Activity(ScaleDemoActivity.class);
                break;
            case R.id.kinds_of_round:
                jump2Activity(RoundDemoActivity.class);
                break;
            case R.id.dynamic_picture:
                jump2Activity(DynamicDemoActivity.class);
                break;
            case R.id.thumbnail_previews:
                jump2Activity(ThumbnailDemoActivity.class);
                break;
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private void jump2Activity(Class<?> clazz) {
        Intent intent = new Intent();
        intent.setClass(this,clazz);
        startActivity(intent);
    }
}
